package cn.virens.util.ztree;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

/**
 * Ztree 节点树
 * 
 * @作者 :loioi
 * @创建时间 :2016年12月19日
 */
public class Node<I> {
	/** 节点ID */
	private I id;
	/** 父节点ID */
	private I pId;
	/** 节点名称 */
	private String name;
	/** 节点图标 */
	private String icon;
	/** 节点图标-展开 */
	private String iconOpen;
	/** 节点图标-关闭 */
	private String iconClose;
	/** 节点图标-CSS */
	private String iconSkin;
	/** 是否被打开 */
	private Boolean open = false;
	/** 子节点异步加载 */
	private Boolean halfCheck;
	/** 子节点 */
	private List<Node<I>> children;

	public Node() {
		super();
	}

	public Node(String name) {
		this.name = name;
	}

	public I getId() {
		return id;
	}

	public void setId(I id) {
		this.id = id;
	}

	public I getpId() {
		return pId;
	}

	public void setpId(I pId) {
		this.pId = pId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getIconOpen() {
		return iconOpen;
	}

	public void setIconOpen(String iconOpen) {
		this.iconOpen = iconOpen;
	}

	public String getIconClose() {
		return iconClose;
	}

	public void setIconClose(String iconClose) {
		this.iconClose = iconClose;
	}

	public String getIconSkin() {
		return iconSkin;
	}

	public void setIconSkin(String iconSkin) {
		this.iconSkin = iconSkin;
	}

	public Boolean getOpen() {
		return open;
	}

	public void setOpen(Boolean open) {
		this.open = open;
	}

	public Boolean getHalfCheck() {
		return halfCheck;
	}

	public void setHalfCheck(Boolean halfCheck) {
		this.halfCheck = halfCheck;
	}

	public List<Node<I>> getChildren() {
		return children;
	}

	public void setChildren(List<Node<I>> children) {
		this.children = children;
	}

	public boolean addChildren(Node<I> node) {
		// 将节点交给下级判断是否是它的子节点
		if (CollectionUtils.isNotEmpty(getChildren())) {
			for (Node<I> n : getChildren()) {
				if (n.addChildren(node)) {
					return true;
				}
			}
		}
		// 不是下级的子节点就判断是否是自己的子节点
		if (id != null && id.equals(node.getpId())) {
			return _addChildren(node);
		}
		// 都不是
		return false;
	}

	protected boolean _addChildren(Node<I> node) {
		if (this.children == null) this.children = new ArrayList<>();

		return this.children.add(node);
	}
}
